import json
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render, redirect
from django.urls import reverse

# Create your views here.

# GET /session_demo/
def session_demo(request):
    """演示session缓存读写"""
    # 设置session session它是依赖cookie
    request.session['name']='xcg'
    # 存储session时，它会自动生成一个session_id
    # 后面会把这个session_id 通过响应写入到浏览cooke中

    # 读取session
    name = request.session.get('name')
    # 通过reques带过的cookie 它里面有session_id
    # 通过session_id就可以取出对应的那条session记录
    # 再通过name key取到xcg
    print(name)

    return HttpResponse('session_demo')


# GET /cookie_demo/
def cookie_demo(request):
    """cookie缓存数据读写"""
    # 设置cookie 需要通过响应对象设置
    response = HttpResponse('cookie_demo')
    # set_cookie(key, value, 过期时间)
    response.set_cookie("name","caoren",max_age=3600)
    # 读取cookie数据 需要通过请求对象
    name = request.COOKIES.get("name")
    print(name)
    return response



# GET / redirect_demo/
def redirect_demo(request):
    # 在路径钱加/表示从跟路径开始拼接
    # return redirect('/users/index/')
    # 重定向配合反向解析
    # url = reverse('users:index')
    # print(url)
    return redirect(reverse('users:index'))



# GET / reverse_demo/
def reverse_demo(request):
    """演示路由的命名空间和反向解析"""
    #正向解析：通过路由找视图
    #反向解析：通过视图找路由
    url = reverse('request_response:reverse_demo')
    print(url)
    return HttpResponse('reverse_demo')

# GET /response_json/
def response_json(request):
    """演示响应json数据"""
    # json中的引号一定要用双引号
    json_dict ={"a":"18","b":"12","c":"10"}
    return JsonResponse(json_dict)


# GET/response_demo/
def response_demo(request):
    """演示相应体常规操作"""
    #return HttpResponse(content=响应体, content_type=响应体数据类型, status=状态码)
    #return HttpResponse('hello',content_type='text/html',status=200)
    #return HttpResponse('hello',content_type='text/html',status=204)
    #return HttpResponse('hello',content_type='text/plain',status=200)
    # 自定义响应头信息
    response = HttpResponse('ok')
    response['itcast']='python'
    return response



# GET /request_head/
def request_head(request):
    """演示获取请求头信息"""
    con_type = request.META['CONTENT_TYPE']
    print(con_type)
    print(request.method,request.path)

    return HttpResponse('request_head')



# POST body/json
def body_json(request):
    """演示提取请求体中的非表单数据 json"""
    bytes_str = request.body # 获取请求体中的非表单数据，类型是字节类型
    json_str = bytes_str.decode() # 将二进制转换成字符串
    json_dict = json.loads(json_str) # 将json格式的字符串转换成json字典
    print(bytes_str)
    print(json_str)
    print(json_dict)
    return HttpResponse('body_json')



# POST /body_form/
def body_form(request):
 """演示提取请求体中的表单数据"""
 # 提取请求体中的表单数据用 POST
 # request.POST 返回一个QueryDict 类型的对象，类似字典
 like = request.POST.get('like')
 b = request.POST.get('b')
 like_list = request.POST.getlist('like')
 print(like)
 print(b)
 print(like_list)

 return HttpResponse('body_form')


def query_str(request):
    """演示提取字符串数据"""
    # request.GET 返回一个QueryDict 类型的对象，类似字典
    # 注意点：request.GET和请求方法无关，只是得到一个QueryDict类对象
    a = request.GET.get('a') # 一键一值，如果有多个值会得到最后一个
    b =request.GET.get('b')
    a_list = request.GET.getlist('a') # 一键多值，返回列表
    print(a)
    print(b)
    print(a_list)
    return HttpResponse('query_str')


# http://127.0.0.1:8000/weather/sz/2018/
def weather(request,city,year):
    print(city)
    print(year)

    return HttpResponse('weather')